# Copyright (C) 2004, 2005  Fletcher T. Penney <fletcher@freeshell.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
#    Free Software Foundation, Inc.
#    59 Temple Place, Suite 330
#    Boston, MA 02111-1307 USA

*OldGetHeader = *GetHeader;
*GetHeader = *CustomGetHeader;

sub CustomGetHeader {
    my ($id, $title, $oldId, $nocache, $status, $rev) = @_;
    my $embed = GetParam('embed', $EmbedWiki);
    my $alt = T('[Home]');
    my ($result, $smarttitle);
    $title = NormalToFree($title); 
    
    if ($oldId) {
        $Message .= $q->p('(' . Ts('redirected from %s', GetEditLink($oldId, $oldId)) . ')');
    }
    
    if ($DataDir =~ m|/tmp/|) {
        $Message .= $q->p('Database is stored in temporary directory');
    }

    $result = GetHttpHeader('text/html', $nocache, $status);

    $result .= GetHtmlHeader("$SiteName: $title", $id);
  
    if ($embed) {
        $result .= $q->div({-class=>'message'}, $Message)  if $Message;
        return $result;
    }
 
    $result .= $q->a({-name=>'top'}); 
    $result .= $q->start_div({-class=>'container', -class=>'container'});
    $result .= $q->start_div({-class=>'container_bg'});
    $result .= $q->start_div({-class=>'container_left'});
    $result .= $q->start_div({-class=>'container_right'});
    $result .= $q->start_div({-class=>'container_top'});
    $result .= $q->start_div({-class=>'clearfix'});
 
    $result .= $q->div({-class=>'theme'}, 
            $q->a({-onclick=>"setActiveStyleSheet('v1'); return false;",-title=>'Theme: Get your Eye Candy On!',-class=>'v1'},'A'),
            $q->a({-onclick=>"setActiveStyleSheet('v2'); return false;",-title=>'Theme: Plane Jane',-class=>'v2'},'A')
    
    );
 
    $result .= $q->start_div({-class=>'header'});

    if (not $embed and $LogoUrl) {
        my $url = $IndexHash{$LogoUrl} ? GetDownloadLink($LogoUrl, 2) : $LogoUrl;
        $result .= ScriptLink(UrlEncode($HomePage), $q->img({-src=>$url, -alt=>$alt, -class=>'logo'}), 'logo');
    }

    $result .= $q->div({-class=>'message'}, $Message)  if $Message;

    if ($id ne '') {
        $result .= $q->h1(GetSearchLink($id, '', '', T('Search for "' . $id . '"')));
    } else {
        $result .= $q->h1($title);
    }
    $result .= $q->end_div(); # End Class Header

    #return $Page{text};

    return $result;
}

*OldPrintFooter = *PrintFooter;
*PrintFooter = *CustomPrintFooter;

sub CustomPrintFooter {
    my ($id, $rev, $comment) = @_;
    if (GetParam('embed', $EmbedWiki)) {
        print $q->end_html;
        return;
    }
    
    FootnoteFooter();

    if (($id ne undef) && ($id !~ /^Comments.*/) && ($id !~ /Links/) && ($id !~ /^Journal/) && (GetParam('action', '') ne 'edit')) {
        GetBackLink($id);
    }

    print GetCommentForm($id, $rev, $comment);
    print GetSearchForm();
 
    print $q->start_div({-class=>'contentfooter'}), $q->end_div();
    print $q->end_div(); # clearfix
    print $q->end_div(); # topline 
    print $q->end_div(); # contentwrap_right
    print $q->end_div(); # contentwrap_left
    print $q->end_div(); # End content_bg 
    print $q->end_div(); # End container

    print $q->start_div({-class=>'footer'});
    print CustomGetGotoBar($id), GetFooterLinks($id, $rev);
    print $q->end_div();
    

    #print T($FooterNote) if $FooterNote;
    #print $q->p(GetValidatorLink()) if GetParam('validate', $ValidatorLink);
    #print $q->p(Ts('%s seconds', (time - $Now))) if GetParam('timing',0);

    SideBarContent($id);

    PrintMyContent($id) if defined(&PrintMyContent);
    print $q->end_html;
}

*OldPrintAllPages = *PrintAllPages;
*PrintAllPages = *CustomPrintAllPages;

sub CustomPrintAllPages {
  my ($links, $comments, @pages) = @_;
  my $lang = GetParam('lang', 0);
  @pages = @pages[0 .. $JournalLimit - 1] if $#pages >= $JournalLimit and not UserIsAdmin();
  for my $id (@pages) {
    OpenPage($id);
    my @languages = split(/,/, $Page{languages});
    next if $lang and @languages and not grep(/$lang/, @languages);
    my $title = NormalToFree($id);
    print $q->start_div({-class=>'page'});
    print $q->h1($links ? GetPageLink($id, $title) : $q->a({-name=>$id},$title));
    PrintPageHtml();
    if ($comments and UserCanEdit($CommentsPrefix . $id, 0) and $id !~ /^$CommentsPrefix/) {
      print $q->p({-class=>'comment'}, GetPageLink($CommentsPrefix . $id, T('Comments on this page')));
    }
    print $q->end_div(); 
  }
}
1;
